<?php
namespace App\Ordersys\Controllers\Admin;
use App\Admin\Models\MemberModel;
use App\Base\Controllers\Controller;
use App\Ordersys\Exceptions\OrdersysException;
use App\Ordersys\Models\LuckDrawAwaerModel;
use App\Ordersys\Models\LuckDrawModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2019/3/19
 * Time: 15:37
 */
class LuckDrawController  extends Controller
{

    /**
     * @return LuckDrawModel
     */
    public function getLuckDrawModel(){
        return app()->make(LuckDrawModel::class);
    }


    /**
     *
     * @return MemberModel
     */
    public function getMemberModel()
    {
        return app()->make(MemberModel::class);
    }

    /**
     * @return LuckDrawAwaerModel
     */
    public function getLuckDrawAwaerModel(){
        return app()->make(LuckDrawAwaerModel::class);
    }
    /**
     * 大转盘添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
     public function add(Request $request){
         if ($request->getMethod() == 'POST') {
           return $this->getLuckDrawModel()->newQuery()->updateOrCreate(["id"=>1],$request->all());
         }
         $info=$this->getLuckDrawModel()->newQuery()->where("id",1)->first();
         if(!empty($info)){
             $info->start_date=date("Y-m-d H:i:s",$info->start_time??time());
             $info->end_date=date("Y-m-d H:i:s",$info->end_time??time());
         }
         return view("ordersys.luckdraw.add")->with("info",$info);
     }

    /**
     * 销售员抽奖
     */
     public function index(){
         $info=$this->getLuckDrawModel()->newQuery()->where("id",1)->first();
         if(!empty($info)){
             $info->start_date=date("Y-m-d H:i:s",$info->start_time??time());
             $info->end_date=date("Y-m-d H:i:s",$info->end_date??time());
         }
         /**
          * 获取用户次数
          */
         $last_num=$this->getMemberModel()->newQuery()->where("uid",UID)->value("award_num");

         $rmsg=$this->return_arr();
         return view("ordersys.luckdraw.index")->with("info",$info)->with("last_num",$last_num)->with("rmsg",$rmsg);
     }
    /**
     * 查看我的中奖记录
     */
    public function show_award(Request $request){
        $rmsg=$this->return_arr();
        if ($request->getMethod() == 'POST') {
            $where=function($query){
            };
            $pagelist=$this->getLuckDrawAwaerModel()->newQuery()->where($where)->orderBy("create_time","desc")->paginate(10);
            if(!empty($pagelist -> items())){
                foreach($pagelist -> items()  as $k => $v){
                    $v["award_name"] = $rmsg[$v->awaer];
                    $data[] = $v;
                    $custom = collect(['data' => $data]) ;
                    }
                   $custom->merge($pagelist);
                }

            return $pagelist;
        }

        return view("ordersys.luckdraw.show_award")->with("rmsg",$rmsg);
    }

    /***
     * 添加到历史记录中去
     * @param Request $request
     */
    public function add_awards(Request $request){
        $info=$this->getMemberModel()->newQuery()->where("uid",UID)->first();
        if($info->award_num<=0){
            throw new OrdersysException(OrdersysException::EROOR_LUCK_NOT_IEXT);
        }
        $d_id=$request->input("d_id");
        $awaer=$request->input("awaer");
        $data=array();
        $data["uid"]=UID;
        $data["awaer"]=$awaer;
        $data["d_id"]=$d_id;
        $data["truename"]=$this->getMemberModel()->newQuery()->where("uid",UID)->value("nickname");
         if($this->getLuckDrawAwaerModel()->create($data)){
               if($this->getMemberModel()->newQuery()->where("uid",UID)->decrement("award_num",1)){
                   return array();
               }
         }
         throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);

    }
    /**
     * 判断机率
     */
    public function testmy(Request $request){
        $id = $request->input("id");
        $d_info = $this->getLuckDrawModel()->newQuery()->where(array("id" => $id))->first();

        $new_arr = array();
        if (!empty($d_info->setaward)) {
            $keyword_arr = explode("\n", $d_info->setaward);
            foreach ($keyword_arr as $kk => $vv) {
                $ex_arr = explode(":", $vv);
                $new_arr[$ex_arr[0]] = $ex_arr[1];
            }
        }

        $rid = $this->get_rand($new_arr);
        echo empty($rid) ? 0 : $rid;
    }
    function get_rand($proArr) {
        $result = '';
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset($proArr);
        return $result;
    }

    public function return_arr(){
        return [
            "很遗憾您未中奖，再接再厉",
            "恭喜您减免36000元",
            "恭喜您获得减免24000元",
            "恭喜您获得减免12000元",
            "恭喜您获得减免6000元",
            "恭喜您获得免费体验券",
            "恭喜您获得免费体验券",
        ];
    }
}
